package net.youdi.gateway.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import net.youdi.gateway.filter.AuthenticationFilter;
import net.youdi.gateway.filter.CorsHeadersFilter;
import net.youdi.gateway.filter.GatewayRequestFilter;
import net.youdi.gateway.handler.DefaultGateWayRequestCacheHandler;
import net.youdi.gateway.handler.GateWayRequestCacheHandler;
import net.youdi.gateway.handler.response.RewriteResponseHandler;
import net.youdi.gateway.handler.response.RewriteResponseHandlerFunction;
import net.youdi.gateway.properties.GatewayProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.codec.Decoder;
import org.springframework.http.codec.DecoderHttpMessageReader;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.json.AbstractJackson2Decoder;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author:scl
 * @date:2019-05-16 13:22
 * @description
 */
@Configuration
@EnableConfigurationProperties(GatewayProperties.class)
public class GateWayConfig {

    @PostConstruct
    private void init(){
        ExchangeStrategies exchangeStrategies = ExchangeStrategies.withDefaults();
        List<HttpMessageReader<?>> httpMessageReaders = exchangeStrategies.messageReaders();
        for (HttpMessageReader<?> httpMessageReader : httpMessageReaders) {
            if (httpMessageReader instanceof DecoderHttpMessageReader){
                DecoderHttpMessageReader decoderHttpMessageReader = (DecoderHttpMessageReader) httpMessageReader;
                Decoder decoder = decoderHttpMessageReader.getDecoder();
                if (decoder instanceof AbstractJackson2Decoder){
                    ((AbstractJackson2Decoder)decoder).setMaxInMemorySize(26214400);
                }
            }
        }
    }
    @Bean
    public RewriteResponseHandlerFunction rewriteResponseHandlerFunction(ObjectMapper objectMapper){
        return new RewriteResponseHandler(objectMapper);
    }
    @Bean
    @ConditionalOnMissingBean(GatewayRequestFilter.class)
    @ConditionalOnProperty(prefix = GatewayProperties.GATEWAY_PLUGIN_PROPERTIES_PREFIX,value = "log-request",havingValue = "true",matchIfMissing = true)
    public GatewayRequestFilter gatewayLogFilter(GatewayProperties gatewayProperties, ObjectMapper objectMapper,
                                                 @Autowired(required = false) GateWayRequestCacheHandler gateWayRequestCacheHandler){
        return new GatewayRequestFilter(objectMapper,gatewayProperties,gateWayRequestCacheHandler);
    }
    /*@Bean
    @ConditionalOnMissingBean(AuthenticationFilter.class)
    public AuthenticationFilter authenticationFilter(WebClient.Builder webClient, GatewayProperties gatewayProperties){
        return new AuthenticationFilter(webClient,gatewayProperties.getAuthentication());
    }*/
    @Bean
    @ConditionalOnMissingBean(CorsHeadersFilter.class)
    public CorsHeadersFilter corsHeadersFilter(){
        return new CorsHeadersFilter();
    }
    @Bean
    @ConditionalOnMissingBean(GateWayRequestCacheHandler.class)
    @ConditionalOnProperty(prefix = GatewayProperties.GATEWAY_PLUGIN_PROPERTIES_PREFIX,value = "log-cache",havingValue = "true",matchIfMissing = true)
    public GateWayRequestCacheHandler gateWayRequestCache(){
        return new DefaultGateWayRequestCacheHandler();
    }
    @Bean
    @LoadBalanced
    public WebClient.Builder webClient(){
        return WebClient.builder();
    }
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }


}
